import { FindAllCarModelDto } from './dto/findAll-car_model.dto';
import { ApiResult } from './../../support/code/ApiResult';
import { CarBrandService } from './../car_brand/car_brand.service';
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CarModelService } from './car_model.service';
import { CreateCarModelDto } from './dto/create-car_model.dto';
import { UpdateCarModelDto } from './dto/update-car_model.dto';

@ApiTags('汽车型号')
@Controller('car_model')
export class CarModelController {
  constructor(
    private readonly carModelService: CarModelService,
    private readonly carBrandService: CarBrandService,
  ) {}

  @ApiOperation({ summary: '创建汽车型号' })
  @Post()
  async create(
    @Body() createCarModelDto: CreateCarModelDto,
  ): Promise<ApiResult> {
    const { brand_id } = createCarModelDto;
    const data = await this.carBrandService.findOne(brand_id);
    if (!data) {
      return ApiResult.ERROR(-1, '未查询到此品牌的信息');
    }
    const result = await this.carModelService.create(createCarModelDto);
    return ApiResult.SUCCESS(result, 0, '创建成功!');
  }

  @ApiOperation({ summary: '获取所有汽车型号' })
  @Get()
  async findAll(
    @Query() findAllCarModelDto: FindAllCarModelDto,
  ): Promise<ApiResult> {
    const result = await this.carModelService.findAll(findAllCarModelDto);
    return ApiResult.SUCCESS(result, 0, '获取成功！');
  }

  @Get(':id')
  async findOne(@Param('id') id: string): Promise<ApiResult> {
    const result = await this.carModelService.findOne(+id);
    return ApiResult.SUCCESS(result, 0, '获取成功！');
  }

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body() updateCarModelDto: UpdateCarModelDto,
  ): Promise<ApiResult> {
    const result = await this.carModelService.update(+id, updateCarModelDto);
    return ApiResult.SUCCESS(result, 0, '修改成功!');
  }

  @Delete(':id')
  async remove(@Param('id') id: string): Promise<ApiResult> {
    const result = await this.carModelService.remove(+id);
    if (result.affected === 0) {
      return ApiResult.ERROR(-1, '请检查是否有此id');
    }
    return ApiResult.SUCCESS(null, 0, '删除成功！');
  }
}
